package com.marquisx.tzdice;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.marquisx.tzdice.model.DiceAction;
import com.marquisx.tzdice.model.DiceActionManager;

public class ActionSetting extends Activity implements OnClickListener
{
	private static final String TAG = "TZDice: ActionSetting";
	
	private LinearLayout basicContainer;
	private LinearLayout backgroundImageContainer;
	
	private DiceActionManager actionManager;

	private GridView actoinSettingGV;
	private Button addBtn;
	private Button clearBtn;
	private Button restoreBtn;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		actionManager = DiceActionManager.getInstance(this);

		setContentView(R.layout.action_setting_grid);
		findViews();
		setListeners();
		prepareViews();
	}
	
	@Override
	public void onResume()
	{
		super.onResume();
		
		this.onCreate(null);
	}

	@Override
	public void onDestroy()
	{
		super.onDestroy();
		actionManager.closeDB();
	}

	private void findViews()
	{
		basicContainer = (LinearLayout) findViewById(R.id.basic_container);
		backgroundImageContainer = (LinearLayout) findViewById(R.id.background_image_container);
		
		actoinSettingGV = (GridView) findViewById(R.id.action_setting_grid);
		addBtn = (Button) findViewById(R.id.add_action_btn);
		clearBtn = (Button) findViewById(R.id.clear_all_action_btn);
		restoreBtn = (Button) findViewById(R.id.restore_action_btn);
	}

	private void prepareViews()
	{
		basicContainer.setBackgroundColor(Setting.getBackgroundColor(this));
		backgroundImageContainer.setBackgroundResource(Setting.getBackgroundPictureId(this));
		if( null != backgroundImageContainer.getBackground() )
		{
			backgroundImageContainer.getBackground().setAlpha(Constants.ALPHA_BG_PIC);
		}
		setTitle(getText(R.string.app_name) + " " + getText(R.string.app_version_name) + " - " + getText(R.string.action_setting_title));
		
		List<DiceAction> actionList = actionManager.getActionList();
		actoinSettingGV.setAdapter(new ActionSettingAdapter(this, actionList));
	}

	private void setListeners()
	{
		addBtn.setOnClickListener(this);
		clearBtn.setOnClickListener(this);
		restoreBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v)
	{
		switch(v.getId())
		{
			case R.id.add_action_btn:
				doAdd(this, actionManager);
				break;
			case R.id.clear_all_action_btn:
				doClearAll(this, actionManager);
				onCreate(null);
				break;
			case R.id.restore_action_btn:
				doRestore(this, actionManager);
				onCreate(null);
				break;
		}
	}
	
	private void doEdit(DiceAction diceAction)
	{
		getSharedPreferences(Constants.PREF, 0)
			.edit()
			.putLong(Constants.SELECTED_ACTION_ID, diceAction.getId())
			.commit();
		
		startActivity(new Intent(this, ActionEdit.class));
	}
	
	public static void doAdd(Activity activity, DiceActionManager actionManager)
	{
		int maximum;
		if( activity.getResources().getBoolean(R.bool.app_version_pro))
		{
			maximum = activity.getResources().getInteger(R.integer.app_action_maximum_pro);
		}
		else
		{
			maximum = activity.getResources().getInteger(R.integer.app_action_maximum);
		}
		
		//Create a new action an then put it into action edit screen
		if( actionManager.getActionNumber() < maximum )
		{
			//Save user preferences. use Editor object to make changes.
			activity.getSharedPreferences(Constants.PREF, 0)
				.edit()
				.putLong(Constants.SELECTED_ACTION_ID, Constants.NEW_ACTION_ID)
				.commit();
			
			activity.startActivity(new Intent(activity, ActionEdit.class));
		}
		else
		{
			Toast.makeText(activity, R.string.msg_action_maximum_reach, Toast.LENGTH_SHORT).show();
		}
	}

	private void doRemove(final DiceAction diceAction)
	{
		new AlertDialog.Builder(this)
		.setTitle(R.string.label_warning)
		.setMessage(R.string.msg_remove_action)
		.setIcon(android.R.drawable.ic_dialog_alert)
		.setPositiveButton(R.string.label_confirm,
				new DialogInterface.OnClickListener()
				{
					public void onClick(
						DialogInterface dialogInterface, int i)
						{
							actionManager.removeAction(diceAction);
							ActionSetting.this.onCreate(null);
						}
				})
		.setNegativeButton(R.string.label_cancel, 
				new DialogInterface.OnClickListener()
				{
					public void onClick(DialogInterface dialog, int which)
					{
					}
				})
		.show();
	}

	public static void doRestore(Activity activity, final DiceActionManager actionManager)
	{
		new AlertDialog.Builder(activity)
		.setTitle(R.string.label_warning)
		.setMessage(R.string.msg_restore_action)
		.setIcon(android.R.drawable.ic_dialog_alert)
		.setPositiveButton(R.string.label_confirm,
				new DialogInterface.OnClickListener()
				{
					public void onClick(
						DialogInterface dialogInterface, int i)
						{
							actionManager.removeAllActions();
							actionManager.addActions(actionManager.getDefaultDiceActions());
							
						}
				})
		.setNegativeButton(R.string.label_cancel, 
				new DialogInterface.OnClickListener()
				{
					public void onClick(DialogInterface dialog, int which)
					{
					}
				})
		.show();
	}

	public static void doClearAll(Activity activity, final DiceActionManager actionManager)
	{
		new AlertDialog.Builder(activity)
			.setTitle(R.string.label_warning)
			.setMessage(R.string.msg_clear_all_action)
			.setIcon(android.R.drawable.ic_dialog_alert)
			.setPositiveButton(R.string.label_confirm,
					new DialogInterface.OnClickListener()
					{
						public void onClick(
							DialogInterface dialogInterface, int i)
							{
								actionManager.removeAllActions();
							}
					})
			.setNegativeButton(R.string.label_cancel, 
					new DialogInterface.OnClickListener()
					{
						public void onClick(DialogInterface dialog, int which)
						{
						}
					})
			.show();
	}
	
	private class ActionSettingAdapter extends BaseAdapter
	{
		private List<DiceAction> actionList;
		private Context mContext;
		
		public ActionSettingAdapter(Context c, List<DiceAction> actionList)
		{
			mContext = c;
			this.actionList = actionList;
		}

		public int getCount()
		{
			return actionList == null ? 0 : actionList.size();
		}

		public Object getItem(int position)
		{
			return position;
		}

		public long getItemId(int position)
		{
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent)
		{
			final DiceAction action = actionList.get(position);
			View actionView = LayoutInflater.from(mContext).inflate(R.layout.action_widget_01, null);
			
			ImageView actionPicIV = (ImageView)actionView.findViewById(R.id.action_setting_pic);
			actionPicIV.setBackgroundResource(action.getPictureId());
			
			TextView actionStatusTV = (TextView)actionView.findViewById(R.id.action_setting_status);
			actionStatusTV.setText(action.getStatus());
			
			TextView actionNameTV = (TextView)actionView.findViewById(R.id.action_setting_name);
			actionNameTV.setText(action.getName());
			
			TextView actionDescriptionTV = (TextView)actionView.findViewById(R.id.action_setting_description);
			actionDescriptionTV.setText(action.getDescription());
			
			ImageView dicePicIV = (ImageView)actionView.findViewById(R.id.action_dice_pic);
			dicePicIV.setImageResource(action.getDiceType().getDicePicId());
			dicePicIV.setAlpha(Constants.ALPHA_DICE_BG);			
			
			actionView.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					doEdit(action);
				}
			});
			
			actionView.setOnLongClickListener(new OnLongClickListener()
			{
				@Override
				public boolean onLongClick(View v)
				{
					doRemove(action);
					return true;
				}
			});
			
			return actionView;
		}
	}

}
